<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>JSONP Memory Leak | 在路上</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="什么是JSONP？JSONP （JSON with Padding）是资料格式 JSON 的一种“使用模式”，可以让网页从别的网域要资料。另一个解决这个问题的新方法是跨来源资源共享。 由于 同源策略 ，一般来说位于 server1.example.com 的网页无法与不是 server1.example.com 的服务器沟通，而 HTML 的  script  元素是一个例外。利用 script元">
<meta name="keywords" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="JSONP Memory Leak">
<meta property="og:url" content="http://yoursite.com/2011/10/22/JSONP-Memory-Leak/index.html">
<meta property="og:site_name" content="在路上">
<meta property="og:description" content="什么是JSONP？JSONP （JSON with Padding）是资料格式 JSON 的一种“使用模式”，可以让网页从别的网域要资料。另一个解决这个问题的新方法是跨来源资源共享。 由于 同源策略 ，一般来说位于 server1.example.com 的网页无法与不是 server1.example.com 的服务器沟通，而 HTML 的  script  元素是一个例外。利用 script元">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-03-26T11:35:06.675Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JSONP Memory Leak">
<meta name="twitter:description" content="什么是JSONP？JSONP （JSON with Padding）是资料格式 JSON 的一种“使用模式”，可以让网页从别的网域要资料。另一个解决这个问题的新方法是跨来源资源共享。 由于 同源策略 ，一般来说位于 server1.example.com 的网页无法与不是 server1.example.com 的服务器沟通，而 HTML 的  script  元素是一个例外。利用 script元">
  
    <link rel="alternate" href="/atom.xml" title="在路上" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">在路上</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">一位崇尚价值投资的 Web 开发者</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-JSONP-Memory-Leak" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2011/10/22/JSONP-Memory-Leak/" class="article-date">
  <time datetime="2011-10-21T16:00:00.000Z" itemprop="datePublished">2011-10-22</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      JSONP Memory Leak
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="什么是JSONP？"><a href="#什么是JSONP？" class="headerlink" title="什么是JSONP？"></a>什么是JSONP？</h2><p><code>JSONP</code> （JSON with Padding）是资料格式 JSON 的一种“使用模式”，可以让网页从别的网域要资料。另一个解决这个问题的新方法是跨来源资源共享。</p>
<p>由于 <code>同源策略</code> ，一般来说位于 server1.example.com 的网页无法与不是 server1.example.com 的服务器沟通，而 HTML 的  <code>script</code>  元素是一个例外。利用 script元素的这个开放策略，网页可以得到从其他来源动态产生的 JSON 资料，而这种使用模式就是所谓的 JSONP。用 JSONP 抓到的资料并不是 JSON，而是任意的 JavaScript，用 JavaScript 直译器执行而不是用 JSON 解析器解析。</p>
<p>以上内容摘抄自：<a href="http://zh.wikipedia.org/wiki/JSONP" target="_blank" rel="noopener">JSONP@维基百科</a></p>
<p>简而言之，JSONP就是WEB前端开发中最常用的一种 <code>跨域请求</code> 数据的方式。</p>
<h2 id="使用场景"><a href="#使用场景" class="headerlink" title="使用场景"></a>使用场景</h2><p>JSONP是一种常用的跨域请求脚本的方式。如果页面不涉及轮询，那也不是什么大问题。但是当页面中存在 <code>轮询</code> 跨域请求时，问题就被无数倍的放大了。所以需要额外小心由此带来的 <code>内存泄漏</code>！</p>
<p>当然毫无疑问，IE系列始终是最让人纠结的。</p>
<h2 id="跨域加载脚本"><a href="#跨域加载脚本" class="headerlink" title="跨域加载脚本"></a>跨域加载脚本</h2><p>通常我们使用下面的方式来加载一段脚本：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</span><br><span class="line">script.src = <span class="string">'http://www.abc.com/somepage?callback=check'</span>;</span><br><span class="line">script.id = <span class="string">'JSONP'</span>;</span><br><span class="line"><span class="keyword">var</span> head = <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>];</span><br><span class="line">head.appendChild(script);</span><br></pre></td></tr></table></figure>
<p>对于一般的WEB应用程序这并不存在什么问题，但是一旦你的应用程序中使用到了轮询，这样只增加节点而不删除，将导致内存不断增长，增长的比例和加载脚本执行的内容也有很大的关系。读者可以尝试向页面不断载入jquery源代码，设定轮询间隔为2s，可以看到内存的增速达到几十M每秒。</p>
<p>所以十分有必要在脚本执行完成以后删除这些script节点。</p>
<h2 id="删除加载的script节点"><a href="#删除加载的script节点" class="headerlink" title="删除加载的script节点"></a>删除加载的script节点</h2> <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</span><br><span class="line">script.src = <span class="string">'http://www.abc.com/somepage?callback=check'</span>;</span><br><span class="line">script.id = <span class="string">'JSONP'</span>;</span><br><span class="line">script.type = <span class="string">'text/javascript'</span>;</span><br><span class="line">script.charset = <span class="string">'utf-8'</span>;</span><br><span class="line"><span class="keyword">var</span> head = <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>];</span><br><span class="line">head.appendChild(script);</span><br><span class="line">head.removeChild(script);</span><br></pre></td></tr></table></figure>
<p>这样的方式可以吗？显然不行！</p>
<p>因为加载的脚本还没来的及执行就被删除了，所以我们需要 <code>让加载的脚本先执行一会儿</code> 。</p>
<h3 id="曲线救国"><a href="#曲线救国" class="headerlink" title="曲线救国"></a>曲线救国</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	head.removeChild(script);</span><br><span class="line">&#125;,<span class="number">200</span>);</span><br></pre></td></tr></table></figure>
<p>不得不说，这样的解决方式很不优雅，要是脚本200ms内还没有执行完毕，肯定会出问题。所以需要保证脚本执行完之后自动删除。</p>
<h2 id="加载script后自动删除节点"><a href="#加载script后自动删除节点" class="headerlink" title="加载script后自动删除节点"></a>加载script后自动删除节点</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</span><br><span class="line">head.appendChild(script);</span><br><span class="line"><span class="keyword">if</span>(script.readyState)&#123;</span><br><span class="line">	script.onreadysctatechange =<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="comment">//注意使用this避免内存泄漏</span></span><br><span class="line">		<span class="keyword">if</span>(<span class="keyword">this</span>.readyState == <span class="string">'completed'</span> || <span class="keyword">this</span>.readyState == <span class="string">''</span>loaded)</span><br><span class="line">			<span class="keyword">this</span>.parentNode.removeChild(<span class="keyword">this</span>);</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">	script.onload =<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="comment">//注意使用this避免内存泄漏</span></span><br><span class="line">		<span class="keyword">this</span>.onload = <span class="literal">null</span>;</span><br><span class="line">		<span class="keyword">this</span>.parentNode.removeChild(<span class="keyword">this</span>);</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;</span><br><span class="line">script.src = url;</span><br></pre></td></tr></table></figure>
<p>幸好IE支持 <code>onreadysctatechange</code> 事件，而标准浏览器支持 <code>onload</code> 事件来判断脚本的执行状态。</p>
<h2 id="IE浏览器下的问题"><a href="#IE浏览器下的问题" class="headerlink" title="IE浏览器下的问题"></a>IE浏览器下的问题</h2><p>那么是不是现在我们的问题就解决了呢？</p>
<p>测试后发现这样的解决方案不能完全避免内存的不断增长，标准浏览器包括Chrome随着轮询的都会有内存增长的现象，<br>不过增长程度微乎其微（轮询间隔2m，增长速度也只有4k-8k）。但是IE会有十几到几十K的内存增长。</p>
<p>你会看到我们在script加载完成之后已经将script元素删除了，为什么内存还会不断增长呢？</p>
<p>很简单，这就是 <code>内存泄漏</code> 现象。</p>
<h2 id="重用script节点"><a href="#重用script节点" class="headerlink" title="重用script节点"></a>重用script节点</h2><p>标准浏览器对script标签的处理就是每个script标签的地址（src）只能设置一次，<br>后续的设置能够改变地址，但是对应地址的脚本内容不会执行，不管script标签是页面预留的还是动态插入。</p>
<p>IE下面则很神奇，使用js动态插入的script标签不遵守这一规则。<br>于是我们只需要动态的插入一个id已知的script标签，然后不断地改变它的src，以此方式加载的脚本都会执行。<br>而且这个script节点也不必删除，下次请求继续重用即可。</p>
<p>这样IE下面的内存泄漏问题就解决了！</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> _ = <span class="function"><span class="keyword">function</span>(<span class="params">id</span>)</span>&#123;<span class="keyword">return</span> <span class="built_in">document</span>.getElementById(id);&#125;;</span><br><span class="line">_.ajax = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;;</span><br><span class="line">(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//存储超时或异常的JSONP请求</span></span><br><span class="line">    _.ajax.TIMEOUT_REQUEST = [];</span><br><span class="line">	<span class="comment">//超时设定</span></span><br><span class="line">	_.ajax.TIMEOUT = <span class="number">5000</span>;</span><br><span class="line">	<span class="keyword">if</span>(_.isIE)&#123;</span><br><span class="line">		<span class="comment">//JSONP重用标签的id</span></span><br><span class="line">		_.ajax.SCRIPT_ID = <span class="string">'ie_script_for_jsonp'</span>;</span><br><span class="line">		<span class="comment">//重用标签是否被JSONP请求占用</span></span><br><span class="line">		_.ajax.SCRIPT_USED = <span class="literal">false</span>;</span><br><span class="line">		<span class="comment">//被占用时JSONP请求等待的时间</span></span><br><span class="line">		_.ajax.WAIT_TIME = <span class="number">100</span>;</span><br><span class="line">		<span class="comment">//上次JSONP请求的时间</span></span><br><span class="line">		_.ajax.LAST_USED_TIME = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>), head = <span class="built_in">document</span>.head || <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>];</span><br><span class="line">		script.setAttribute(<span class="string">'id'</span>,_.ajax.SCRIPT_ID);</span><br><span class="line">		script.onreadystatechange = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="keyword">if</span> (<span class="keyword">this</span>.readyState == <span class="string">"loaded"</span> || <span class="keyword">this</span>.readyState == <span class="string">"complete"</span>)&#123;</span><br><span class="line">				_.ajax.SCRIPT_USED = <span class="literal">false</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;;</span><br><span class="line">		head.appendChild(script);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;)();</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @description 轮询JSONP请求调用，IE浏览器采用重用Script节点方式。</span></span><br><span class="line"><span class="comment"> * @param url 手动加上callback参数，自动追加了时间戳</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line">_.ajax.jsonp = <span class="function"><span class="keyword">function</span>(<span class="params">url</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(!navigator.onLine)&#123;</span><br><span class="line">		_.ajax.TIMEOUT_REQUEST.push(url);</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">var</span> script, now = <span class="keyword">new</span> <span class="built_in">Date</span>().getTime(),</span><br><span class="line">		requestUrl = url + (url.indexOf(<span class="string">'?'</span>)&gt;<span class="number">-1</span>?<span class="string">'&amp;timestamp='</span>:<span class="string">'?timestamp='</span>) + now,</span><br><span class="line">		head = <span class="built_in">document</span>.head || <span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>];</span><br><span class="line">	<span class="keyword">if</span>(_.isIE &amp;&amp; _.isIE &lt; <span class="number">9</span>)&#123;</span><br><span class="line">		script = <span class="built_in">document</span>.getElementById(_.ajax.SCRIPT_ID);</span><br><span class="line">		<span class="comment">//节点被占用</span></span><br><span class="line">		<span class="keyword">if</span>(_.ajax.SCRIPT_USED)&#123;</span><br><span class="line">			<span class="keyword">if</span>(_.ajax.LAST_USED_TIME === <span class="number">0</span>)</span><br><span class="line">				_.ajax.LAST_USED_TIME = now;</span><br><span class="line">			<span class="comment">//已经超时</span></span><br><span class="line">			<span class="keyword">if</span>((now - _.ajax.LAST_USED_TIME) &gt; _.ajax.TIMEOUT)&#123;</span><br><span class="line">				_.ajax.LAST_USED_TIME = now;</span><br><span class="line">				<span class="keyword">if</span>(_.ajax.TIMEOUT_REQUEST.length&gt;=<span class="number">1000</span>)</span><br><span class="line">					_.ajax.TIMEOUT_REQUEST.length = <span class="number">0</span>;</span><br><span class="line">				_.ajax.TIMEOUT_REQUEST.push(script.src.split(<span class="string">'&amp;timestamp='</span>)[<span class="number">0</span>]);</span><br><span class="line">				script.src = requestUrl;</span><br><span class="line">			<span class="comment">//没有超时则等待</span></span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;_.ajax.jsonp(url);&#125;,_.ajax.WAIT_TIME);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			_.ajax.SCRIPT_USED = <span class="literal">true</span>;</span><br><span class="line">			_.ajax.LAST_USED_TIME = now;</span><br><span class="line">			script.src = requestUrl;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		script = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</span><br><span class="line">		head.appendChild(script);</span><br><span class="line">		script.onload =	<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="keyword">this</span>.onload = <span class="literal">null</span>;</span><br><span class="line">			<span class="keyword">this</span>.parentNode.removeChild(<span class="keyword">this</span>);</span><br><span class="line">		&#125;;</span><br><span class="line">        script.onerror = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">            _.ajax.TIMEOUT_REQUEST.push(<span class="keyword">this</span>.src);</span><br><span class="line">        &#125;;</span><br><span class="line">		script.src = requestUrl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="测试结果"><a href="#测试结果" class="headerlink" title="测试结果"></a>测试结果</h2><p>对上述代码测试，使用 <code>sieve</code> 结合任务管理器，发现此方法引起的内存泄漏和chrome下面的差不多都在4K左右，测试环境是IE 8。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	setInterval(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		_.ajax.jsonp(<span class="string">'data.js'</span>);</span><br><span class="line">	&#125;,<span class="number">1000</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>data.js 的内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">i++;</span><br></pre></td></tr></table></figure>
<h2 id="后续说明"><a href="#后续说明" class="headerlink" title="后续说明"></a>后续说明</h2><p>其实之所以存在内存泄漏，是因为IE的 <code>removeChild</code> 方法存在内存泄漏的问题。对于这一点 <code>jQuery</code> 的 <code>empty</code> 方法已经考虑到。关于removeChild导致内存泄漏的问题，很早就被发现了，具体请看<a href="http://groups.google.com/group/jquery-dev/browse_thread/thread/4a99f6e9b2e33057/45ce657a48afd43a?pli=1" target="_blank" rel="noopener">这里</a>。</p>
<p>一般的框架都有各自的删除节点的方式来避免内存泄漏问题，下面是 <code>EXT</code> 的处理方式：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">_.dom = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @description 采用ext的处理方式，也可以对删除的元素使用outerHTML=''，但是此方法不通用，某些元素的outerHTML属性只读</span></span><br><span class="line"><span class="comment"> * @param n 要删除的HTML节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">_.dom.remove = <span class="function"><span class="keyword">function</span>(<span class="params">n</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> d;</span><br><span class="line">	<span class="keyword">if</span>(_.isIE)&#123;</span><br><span class="line">        <span class="keyword">if</span>(n &amp;&amp; n.tagName != <span class="string">'BODY'</span>)&#123;</span><br><span class="line">            d = d || <span class="built_in">document</span>.createElement(<span class="string">'div'</span>);</span><br><span class="line">            d.appendChild(n);</span><br><span class="line">            d.innerHTML = <span class="string">''</span>;</span><br><span class="line">        &#125;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">	    <span class="keyword">if</span>(n &amp;&amp; n.parentNode &amp;&amp; n.tagName != <span class="string">'BODY'</span>)&#123;</span><br><span class="line">	        n.parentNode.removeChild(n);</span><br><span class="line">	    &#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>为什么jQuery的 <code>getJSON</code> 在进行跨域请求时仍然存在比较严重的内存泄漏？或许是script节点不同于普通的html元素吧，无法按照上述方式删除吧。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2011/10/22/JSONP-Memory-Leak/" data-id="cjf865uf000048wp1scw0xr9h" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/JavaScript/">JavaScript</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2012/02/06/JavaScript-Template-Engine/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          JavaScript Template Engine
        
      </div>
    </a>
  
  
    <a href="/2010/01/29/JavaScript乱弹之（五）：使用脚本创建动态内容/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">JavaScript乱弹之（五）：使用脚本创建动态内容</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript/">JavaScript</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/JavaScript-解析器/">JavaScript,解析器</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/React/">React</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Redux/">Redux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SlimerJS/">SlimerJS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/代码管理/">代码管理</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/持续集成/">持续集成</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/tags/JavaScript/" style="font-size: 20px;">JavaScript</a> <a href="/tags/JavaScript-解析器/" style="font-size: 10px;">JavaScript,解析器</a> <a href="/tags/React/" style="font-size: 16.67px;">React</a> <a href="/tags/Redux/" style="font-size: 10px;">Redux</a> <a href="/tags/SlimerJS/" style="font-size: 10px;">SlimerJS</a> <a href="/tags/代码管理/" style="font-size: 10px;">代码管理</a> <a href="/tags/持续集成/" style="font-size: 13.33px;">持续集成</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/03/">三月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/02/">二月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">十二月 2015</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/06/">六月 2015</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/05/">五月 2015</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/04/">四月 2015</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/07/">七月 2014</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2014/06/">六月 2014</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/11/">十一月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/10/">十月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/07/">七月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/03/">三月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2012/02/">二月 2012</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2011/10/">十月 2011</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2010/01/">一月 2010</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/1970/01/">一月 1970</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2016/09/09/space/">空格</a>
          </li>
        
          <li>
            <a href="/2016/07/17/parser/">JavaScript Parser资源总结</a>
          </li>
        
          <li>
            <a href="/2016/06/29/slimerjs/">使用SlimerJS将网页输出为PDF</a>
          </li>
        
          <li>
            <a href="/2016/06/26/code-management/">代码管理</a>
          </li>
        
          <li>
            <a href="/2016/06/18/violet/">violet - 值得一试的写作同步小助手</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 Simon Xu<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>